package controller

import (
	"github.com/gin-gonic/gin"
	"net/http"
	"week1-project/model/mysql"
	"week1-project/request"
)

//视频发布

func VideoRelease(c *gin.Context) {
	var videoRelease request.ReleaseReq
	if err := c.ShouldBindJSON(&videoRelease); err != nil {
		c.JSON(http.StatusInternalServerError, gin.H{
			"code": 500,
			"msg":  "服务器内部错误",
			"err":  err.Error(),
		})
		return
	}
	data := mysql.Video{
		VideoTitle:   videoRelease.VideoTitle,
		Subtitle:     videoRelease.Subtitle,
		VideoCover:   videoRelease.VideoCover,
		VideoChannel: videoRelease.VideoChannel,
		VideoArea:    videoRelease.VideoArea,
		IsRecommend:  videoRelease.IsRecommend,
	}
	err := data.Release()
	if err != nil {
		c.JSON(http.StatusBadRequest, gin.H{
			"code": 400,
			"msg":  "视频发布失败",
			"err":  err.Error(),
		})
		return
	}
	if videoRelease.VideoTitle == "" || videoRelease.Subtitle == "" {
		c.JSON(http.StatusBadRequest, gin.H{
			"code": 400,
			"msg":  "视频标题不可为空",
			"err":  err.Error(),
		})
		return
	}
	c.JSON(http.StatusOK, gin.H{
		"code": 200,
		"msg":  "视频发布成功",
		"data": data.ID,
	})
}

//视频列表

func VideoList(c *gin.Context) {
	var videoList request.ListReq
	if err := c.ShouldBindJSON(&videoList); err != nil {
		c.JSON(http.StatusInternalServerError, gin.H{
			"code": 500,
			"msg":  "服务器内部错误",
			"err":  err.Error(),
		})
		return
	}
	data := mysql.Video{
		VideoTitle: videoList.VideoTitle,
	}
	list, err := data.List(videoList.Keyword, videoList.Page, videoList.PageSize)
	if err != nil {
		c.JSON(http.StatusBadRequest, gin.H{
			"code": 400,
			"msg":  "视频展示失败",
			"err":  err.Error(),
		})
		return
	}
	c.JSON(http.StatusOK, gin.H{
		"code": 200,
		"msg":  "视频展示成功",
		"data": list,
	})
}
